<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Config;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     * @description app首次获取所有的相关配置性数据
     *
     */
    public function index()
    {
        // 寺庙标题
        $title = Config::get('site')['name'];
        // 寺庙背景
        $backgroundInfo = Db::name('background')->where('is_default', 1)->limit(1)->find();
        if(!$backgroundInfo){
            $backgroundInfo = Db::name('background')->order('id desc')->limit(1)->find();
        }
        $backgroundUrl = $backgroundInfo['img'];
        // 寺庙佛像
        $buddhaList = Db::name('buddha')->field('id,name,img,start_num,end_num')->where('status', 1)->order('sort_order desc')->select();
        // 供灯时间
        $timeSpanList = Db::name('timespan')->field('id,name,month,money')->where('status', 1)->order('sort_order asc')->select();
        // 祈福语
        $blessingList = Db::name('blessing')->field('id,content')->where('status', 1)->order('sort_order desc')->select();
        // 佛灯列表状态列表(根据分页展示,佛像类型进行查询)
        // $light = Db::name('light')->field('id,content')->where('status', 1)->order('sort_order desc')->select();
        // 打印机配置信息
        $printerOptions = [];
        $buddhaSetting = [            
            'BUDDHA_BEGIN_NUM'=> Config::get('site')['BUDDHA_BEGIN_NUM'],
            'BUDDHA_END_NUM'=> Config::get('site')['BUDDHA_END_NUM'],
        ];

        // 查询订单中正在使用的佛灯，且为过期的佛灯编号列表
        // SELECT buddha_no FROM fa_payment WHERE valid_time > UNIX_TIMESTAMP() and order_status= 1;
        $usedBuddhaList = Db::name('payment')->field('buddha_no')->where('valid_time', '>', time())->where('order_status', 1)->where('pay_status',1)->select();
        
        $data = [
            'title'             =>  $title,
            'backgroundUrl'     =>  $backgroundUrl,
            'buddhaList'        =>  $buddhaList,
            'blessingList'      =>  $blessingList,
            'timeSpanList'      =>  $timeSpanList,
            'printerOptions'    =>  $printerOptions,
            'buddhaSetting'     =>  $buddhaSetting,
            'usedBuddhaList'    =>  $usedBuddhaList        
        ];
        $this->success('请求成功', $data);
    }

    /**
     * 获取佛灯状态
     */
    public function getLights()
    {
        $page       =   1;
        $pageSize   =   100;
        $lightList  =   [];
        $data = [
            'list'      =>  $lightList,
            'total'     =>  100,
            'page'      =>  $page
        ];
        $this->success('请求成功', $data);
    }
    
    /**
     * 获取祈福语
     */
    public function getBlessings()
    {

    }

    /**
     * 获取佛像类型
     */
    public function getBuddhas()
    {

    }

    /**
     * 获取打印机配置
     */   
    public function getPrinterSetting()
    {

    } 

    /**
     * 打印小票
     * 编写重复算法，防止重复提交
     */
    public function printOrder()
    {

    }

}
